package com.hhh.springai_test.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhh.springai_test.common.BaseResponse;
import com.hhh.springai_test.common.ErrorCode;
import com.hhh.springai_test.common.ResultUtils;
import com.hhh.springai_test.constant.UserConstant;
import com.hhh.springai_test.exception.BusinessException;
import com.hhh.springai_test.model.dto.book.AddBookDTO;
import com.hhh.springai_test.model.dto.book.BookQueryRequest;
import com.hhh.springai_test.model.dto.book.UpdateBookDTO;
import com.hhh.springai_test.model.dto.book.CategoryStatisticsDTO;
import com.hhh.springai_test.model.po.Book;
import com.hhh.springai_test.service.BookService;
import com.hhh.springai_test.service.BookCategoryService;
import cn.dev33.satoken.annotation.SaCheckRole;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {

    @Resource
    private BookService bookService;

    @Resource
    private BookCategoryService bookCategoryService;

    /**
     * 添加图书
     *
     * @param addBookDTO
     * @return
     */
    @PostMapping("/add")
    @SaCheckRole(UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addBook(@RequestBody AddBookDTO addBookDTO) {
        String loginUserId  =(String) StpUtil.getLoginId();
        // 如果用户ID为空，说明用户未登录，抛出异常
        if(StrUtil.isEmpty(loginUserId)){
            throw new BusinessException(ErrorCode.NO_LOGIN_CODE);
        }
        long bookId = bookService.addBook(addBookDTO, loginUserId);
        return ResultUtils.success(bookId);
    }

    /**
     * 删除图书
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    @SaCheckRole(UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteBook(@PathVariable long id) {
        boolean result = bookService.deleteBook(id);
        return ResultUtils.success(result);
    }

    /**
     * 更新图书
     *
     * @param updateBookDTO
     * @return
     */
    @PostMapping("/update")
    @SaCheckRole(UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateBook(@RequestBody UpdateBookDTO updateBookDTO) {
        boolean result = bookService.updateBook(updateBookDTO);
        return ResultUtils.success(result);
    }

    /**
     * 根据id获取图书
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public BaseResponse<Book> getBookById(@PathVariable long id) {
        Book book = bookService.getById(id);
        if (book == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"图书不存在");
        }
        return ResultUtils.success(book);
    }

    /**
     * 分页获取图书列表
     *
     * @param bookQueryRequest
     * @return
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<Book>> listBookByPage(@RequestBody BookQueryRequest bookQueryRequest) {
        Page<Book> bookPage = bookService.listBookByPage(bookQueryRequest);
        return ResultUtils.success(bookPage);
    }

    /**
     * 获取图书分类统计信息
     *
     * @return 分类统计信息列表
     */
    @GetMapping("/category/statistics")
    public BaseResponse<List<CategoryStatisticsDTO>> getCategoryStatistics() {
        List<CategoryStatisticsDTO> statistics = bookService.getCategoryStatistics();
        return ResultUtils.success(statistics);
    }
}
